/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.emu.OnlineStatus;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipmentType;
import com.thinkgem.jeesite.modules.equipment.entity.EleMonitorParam;
import com.thinkgem.jeesite.modules.equipment.entity.Floor;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentService;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentTypeService;
import com.thinkgem.jeesite.modules.equipment.service.EleMonitorParamService;
import com.thinkgem.jeesite.modules.publish.entity.EleSourceGroupPublish;
import com.thinkgem.jeesite.modules.publish.service.EleSourceGroupPublishService;
import com.thinkgem.jeesite.modules.source.entity.EleSource;
import com.thinkgem.jeesite.modules.source.entity.EleSourceGroup;
import com.thinkgem.jeesite.modules.source.entity.program.EleSourceProgram;
import com.thinkgem.jeesite.modules.source.service.EleSourceGroupService;
import com.thinkgem.jeesite.modules.source.service.program.EleSourceProgramService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.SwitchUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 设备管理Controller
 * @author ZC
 * @version 2017-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleEquipment")
public class EleEquipmentController extends BaseController {

    @Autowired
    private OfficeService officeService;
	@Autowired
	private EleEquipmentService eleEquipmentService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private EleMonitorParamService eleMonitorParamService;

		@Autowired
		private EleEquipmentTypeService eleEquipmentTypeService;
	@ModelAttribute
	public EleEquipment get(@RequestParam(required=false) String id) {
		EleEquipment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleEquipmentService.get(id);
		}
		if (entity == null){
			entity = new EleEquipment();
		}
		return entity;
	}

    /**
     * 本人申请记录
     * @param eleEquipment
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("equipment:eleEquipment:edit")
    @RequestMapping("applylist")
    public String applylist(EleEquipment eleEquipment,  String isFirst,boolean flag, HttpServletRequest request, HttpServletResponse response, Model model){
        User thisUser = UserUtils.getUser();
        	// 申请记录
        eleEquipment.setCreateBy(thisUser);

        eleEquipment.setCreateBy(new User(UserUtils.getUser().getId()));
        if(StringUtils.isEmpty(isFirst)){
        	eleEquipment.setDelFlag(User.DEL_FLAG_AUDIT);
		}
        Page<EleEquipment> page = eleEquipmentService.findPage(new Page<EleEquipment>(request, response), eleEquipment);
    	Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
        model.addAttribute("page", page);
        model.addAttribute("flag", flag);
		model.addAttribute("isFirst", "no");

        return "modules/equipment/myEleEquipmentList";
    }

	/**
	 * 审核记录
	 * @param eleEquipment
	 * @param isFirst
	 * @param flag
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
    @RequiresPermissions("equipment:eleEquipment:check")
    @RequestMapping("mychecklist")
    public String checklist(EleEquipment eleEquipment,String isFirst, boolean flag, HttpServletRequest request, HttpServletResponse response, Model model){
		User thisUser = UserUtils.getUser();
		if (StringUtils.isEmpty(isFirst)) {
			eleEquipment.setDelFlag(User.DEL_FLAG_AUDIT);
		}
		List<User> branches = this.getSubListForUserForCheck(thisUser);
		Page<EleEquipment> page = new Page<>();
		if (!branches.isEmpty()) {
			page = eleEquipmentService.findPageForCheck(eleEquipment, new Page<EleEquipment>(request, response), branches,
					eleEquipment.getDelFlag());
		}
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		model.addAttribute("flag", flag);
		model.addAttribute("isFirst", "no");

		return "modules/equipment/myEleEquipmentCheckList";
    }
    
    @RequiresPermissions("equipment:eleEquipmentType:check")
	@RequestMapping("checkEquipment")
	@ResponseBody
    public Result check(String flag, HttpServletRequest request, String comment){
	    String equipmentList = request.getParameter("groupList");
	    String equipments = equipmentList.substring(0, equipmentList.length() - 1);
	    System.out.println(equipments + "1111");
	    String status;
	    String msg;

	    if("delete".equals(flag)){
		    status = EleSourceGroup.DEL_FLAG_DELETE;
			String locale = I18nUtils.getLocaleStr();
			msg = "删除成功";
			if(Global.US.equals(locale)){
				msg = "Deleted";
			}
		    eleEquipmentService.delApprovalEquipmentStatus(equipments, status);
		    return new Result(Result.SUCCESS, msg, null);
	    }

	    User thisuser = UserUtils.getUser();
	    if("reject".equals(flag)){
		    status = EleSourceGroup.DEL_FLAG_AUDIT_NO;
			String locale = I18nUtils.getLocaleStr();
			msg = "未通过审核";
			if(Global.US.equals(locale)){
				msg = "Not passed";
			}
	    }
	    else{
		    status = EleSourceGroup.DEL_FLAG_NORMAL;
			String locale = I18nUtils.getLocaleStr();
			 msg = "审核通过";
			if(Global.US.equals(locale)){
				msg = "passed";
			}
	    }
	    eleEquipmentService.bathcUpdateEquipmentStatus(equipments, status, thisuser.getId(), comment);
	    return new Result(Result.SUCCESS, msg, null);
    }
    
	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleEquipment eleEquipment,HttpServletRequest request, Model model) {
        List<Office> listTree = new ArrayList<>();

        User current = UserUtils.getUser();
       
		List<User> resultList = getBranchForUser(current);
		Set<String> ids = new HashSet<String>();
		System.out.println("-------------------------------------------------");
		for(User temuser:resultList){
			System.out.println(temuser.getLoginName());
			 String officeId = temuser.getOffice().getId();
			 String companyId = temuser.getCompany().getId();
			ids.add(officeId);
			ids.add(companyId);
		}
		List<Office> listOffice = officeService.findList(true);
		
		Set<String> aimIds = new HashSet<>();
		List<EleEquipment> aimEquipments = eleEquipmentService.findList(eleEquipment);
		for(EleEquipment eq:aimEquipments){
			aimIds.add(eq.getId());
		}
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request); 
		Set<String> officeIds = new HashSet<>();
		Set<String> eqIds = new HashSet<>();
		for(User tmp:resultList){
			EleEquipment eleEquipmentTmp = new EleEquipment();
			eleEquipmentTmp.setCompany(tmp.getCompany());
			eleEquipmentTmp.setOffice(tmp.getOffice());

			List<EleEquipment> listEquipment = eleEquipmentService.findList(eleEquipmentTmp);

			for (EleEquipment equipment : listEquipment) {
				officeIds.add(equipment.getCompany().getId());
				officeIds.add(equipment.getOffice().getId());
				if(eqIds.contains(equipment.getId())){
					continue;
				}
				if(!aimIds.contains(equipment.getId())){
					continue;
				}
				eqIds.add(equipment.getId());
				Office Office = new Office();
				Office.setId(equipment.getId());
				Office.setParent(new Office(equipment.getOffice().getId()));
				Office.setAddress(equipment.getCode());
				EleEquipmentType equipmentType = equipment.getType();
				if(null != equipmentType){
					Office.setEmail(equipmentType.getName());
				}
				Office.setFax(equipment.getWidth() + " X " + equipment.getHeight());
				Office.setRemarks(equipment.getRemarks());
				if(locale.getLanguage() == Locale.CHINESE.getLanguage()) {
					if(OnlineStatus.Online.getIndex().equals(equipment.getState())){
						Office.setGrade(OnlineStatus.Online.getName());
					}else if(OnlineStatus.Publishing.getIndex().equals(equipment.getState())){
						Office.setGrade(OnlineStatus.Publishing.getName());
					}else{
						Office.setGrade(OnlineStatus.Offline.getName());
					}
				}else {
					if(OnlineStatus.Online.getIndex().equals(equipment.getState())){
						Office.setGrade(OnlineStatus.Online.name());
					}else if(OnlineStatus.Publishing.getIndex().equals(equipment.getState())){
						Office.setGrade(OnlineStatus.Publishing.name());
					}else{
						Office.setGrade(OnlineStatus.Offline.name());
					}
				}
				Office.setPhone("");
				Office.setZipCode(equipment.getName());
				listTree.add(Office);
			}
		}
		System.out.println(listTree.size()+"BBBBBBBBBBBBBBBBBB");
		Set<String> resultSet = new HashSet<String>();
		for (String oid : officeIds) {
			resultSet.add(oid);
			Office tmp = officeService.get(oid);
			for (int i = 0; i < listOffice.size(); i++) {
				Office item = listOffice.get(i);
				if (item.getId().equals(tmp.getParentId())) {
					System.out.println(item);
					tmp = item;
					i = -1;
					resultSet.add(item.getId());
				}
			}
		}
		for(String oid:resultSet){
			Office item = officeService.get(oid);
			item.setAddress(null);
			item.setEmail(null);
			item.setFax(null);
			item.setRemarks(null);
			item.setGrade(null);
			item.setPhone("display:none;");
			item.setZipCode("");
			listTree.add(item);
		}
		model.addAttribute("list", listTree);
		return "modules/equipment/eleEquipmentList";
	}

	private List<User> getBranchForUser(User current){	
			User user = new User();
			List<User> list = systemService.findList(user);
			Role role = current.getRole();
			List<User> resultList = new ArrayList<User>();
			if (current.getUserType().equals("1")) {
				resultList = list;
			} else if (current.getUserType().equals("4")) {
				resultList.add(current);
			} else if (current.getUserType().equals("3")) {
				for (User usr : list) {
					if (usr.getUserType().equals("4")
							&& usr.getCompany().getId().equals(current.getOffice().getId())) {
						resultList.add(usr);
					}
				}
				resultList.add(current);
			} else {
				Set<String> offices = new HashSet<String>();
				for (User usr : list) {
					if (usr.getUserType().equals("3")
							&& usr.getCompany().getId().equals(current.getOffice().getId())) {
						resultList.add(usr);
						offices.add(usr.getOffice().getId());
					}
				}
				offices.add(current.getOffice().getId());
				for (User usr : list) {
					if (usr.getUserType().equals("4") && offices.contains(usr.getCompany().getId())) {
						resultList.add(usr);
					}
				}
				resultList.add(current);
			}
			return resultList;
	}
	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = "form")
	public String form(EleEquipment eleEquipment, Model model) {
        User thisuser = UserUtils.getUser();
        // eleEquipment.setCompany(thisuser.getCompany());
		// 审核控制没有关闭 并且没有用户权限的时候
        if (!UserUtils.getSubject().isPermitted("equipment:eleEquipment:check")){
            eleEquipment.setOffice(thisuser.getOffice());
        }
		model.addAttribute("eleEquipment", eleEquipment);
		model.addAttribute("currentUser", thisuser);
		return "modules/equipment/eleEquipmentForm";
	}
	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = "edit")
	public String edit( Model model,HttpServletRequest request) {
		String code = request.getParameter("code").toString();
		String typeId = request.getParameter("typeId").toString();
		String companyId = request.getParameter("companyId").toString();
		String officeId = request.getParameter("officeId").toString();
		String name = request.getParameter("name").toString();
		EleEquipment eleEquipment = new EleEquipment();
		EleEquipmentType type = eleEquipmentTypeService.get(typeId);
		eleEquipment.setCode(code);
		eleEquipment.setType(type);
		eleEquipment.setCompany(officeService.get(companyId));
		eleEquipment.setOffice(officeService.get(officeId));
		eleEquipment.setName(name);
		eleEquipment.setOrientation(type.getOrientation());
		eleEquipment.setWidth(type.getWidth());
		eleEquipment.setHeight(type.getHeight());
        User thisuser = UserUtils.getUser();
        // eleEquipment.setCompany(thisuser.getCompany());
		// 审核控制没有关闭 并且没有用户权限的时候
        if (!UserUtils.getSubject().isPermitted("equipment:eleEquipment:check")){
            eleEquipment.setOffice(thisuser.getOffice());
        }
		model.addAttribute("eleEquipment", eleEquipment);
		model.addAttribute("currentUser", thisuser);
		return "modules/equipment/eleEquipmentForm";
	}
    @RequestMapping(value = "info")
    public String info(EleEquipment eleEquipment, Model model,HttpServletRequest request) {
        model.addAttribute("eleEquipment", eleEquipment);
    	String showType = request.getParameter("showtype");
		if("2".equals(showType)){
			model.addAttribute("show", "no");
		}else{
			model.addAttribute("show", "yes");
		}
        return "modules/equipment/eleEquipmentInfo";
    }

	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "save")
	public String save(EleEquipment eleEquipment, Model model,RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, eleEquipment)){
			return form(eleEquipment, model);
		}
        User thisuser = UserUtils.getUser();
        eleEquipment.setCreateBy(UserUtils.getUser());
        eleEquipment.setCheckDate(new Date());
     
        // 审核控制没有关闭 并且没有用户权限的时候
        if (!UserUtils.getSubject().isPermitted("equipment:eleEquipment:check") && !SwitchUtils.isClose("equipment:eleEquipment:check")){
            eleEquipment.setCheckBy(UserUtils.getCheckUser(""));
            eleEquipment.setDelFlag(EleEquipment.DEL_FLAG_AUDIT);
        }else{
            eleEquipment.setCheckBy(thisuser);
            eleEquipment.setDelFlag(EleEquipment.DEL_FLAG_NORMAL);
        }
        if(StringUtils.isEmpty(eleEquipment.getId())){
            model.addAttribute("operation", "add");
    	}else{
            model.addAttribute("operation", "edit");
    	}
        model.addAttribute("eleEquipment", eleEquipment);

		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
		model.addAttribute("language", locale.getLanguage());

		UserUtils.putCache(UserUtils.getUser().getId()+"_CREATE_EQUIPMENT", eleEquipment);
//		return "modules/equipment/eleEquipmentSave";
		// Edited By Jyz (2018.5)
		return formsave(model);
	}

	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "formsave")
	public String formsave(Model model) {
		System.out.println("1111111111111111111111111111111111111");
		User thisuser = UserUtils.getUser();
		EleEquipment equipment = (EleEquipment)UserUtils.getCache(UserUtils.getUser().getId()+"_CREATE_EQUIPMENT");
		// 保存设备信息
		System.out.println(equipment.getId());
        try{
        	if(StringUtils.isEmpty(equipment.getId())){
                model.addAttribute("operation", "add");
        	}else{
                model.addAttribute("operation", "edit");
        	}
        	if(!isNumeric(equipment.getOrientation())) {
        		equipment.setOrientation(DictUtils.getDictValue(equipment.getOrientation(), "orientation", "1"));
        	}
            eleEquipmentService.save(equipment);
			//eleEquipmentService.updatePublishStatus(equipment.getId(), "1");

            model.addAttribute("result", "success");
        }catch(Exception e){
            logger.error("保存设备信息失败:", e);
            model.addAttribute("result", "fail");
        }
        model.addAttribute("equipment", equipment);
		UserUtils.removeCache(UserUtils.getUser().getId()+"_CREATE_EQUIPMENT");
		return "modules/equipment/eleEquipmentInfoConfirmTips";
	}
	private boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
		}

    @RequiresPermissions("equipment:eleEquipment:edit")
    @RequestMapping(value = "config")
    public String config(EleEquipment equipment, RedirectAttributes redirectAttributes,Model model,HttpServletRequest request) {
		System.out.println("222222222222222222222222222222222222222222222");

        eleEquipmentService.saveConfig(equipment);
		eleEquipmentService.updatePublishStatus(equipment.getId(), "2");
		List<EleMonitorParam> params = eleMonitorParamService.findEleMonitorParamByEqId(equipment.getType().getId());
		model.addAttribute("params", params);
		model.addAttribute("language", RequestContextUtils.getLocaleResolver(request).resolveLocale(request).getLanguage());

		String locale = I18nUtils.getLocaleStr();
		String msg = "保存设备配置成功";
		if(Global.US.equals(locale)){
			msg = "Equipment Monitor saved";
		}
		addMessage(redirectAttributes, msg);
    /*    addMessage(redirectAttributes, "保存设备配置成功!");*/
        return "modules/equipment/eleEquipmentMonitor";
    }
	
	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "delete")
	public String delete(EleEquipment eleEquipment, RedirectAttributes redirectAttributes) {
		eleEquipmentService.delete(eleEquipment);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除设备成功";
		if(Global.US.equals(locale)){
			msg = "equipment deleted";
		}
		addMessage(redirectAttributes, msg);
		/*addMessage(redirectAttributes, "删除设备成功");*/
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipment/?repage";
	}
	@RequiresPermissions("equipment:eleEquipmentType:edit")
	@RequestMapping("resubmit")
	@ResponseBody
	public Result resubmitEquipmentType(String typeList, HttpServletRequest request, HttpServletResponse response, Model model) {
		String types = typeList.substring(0, typeList.length() - 1);
		String status = EleEquipment.DEL_FLAG_AUDIT;
		String msg = "已重新提交审核";
		System.out.println(types);
		User thisuser = UserUtils.getUser();
		eleEquipmentService.bathcUpdateEquipmentStatus(types, status, thisuser.getId(), "");
		return new Result(Result.SUCCESS, msg, null);
	}
	@RequiresPermissions("equipment:eleEquipment:edit")
	@RequestMapping(value = "recheck")
	public String recheck(String id, RedirectAttributes redirectAttributes){
		EleEquipment equipment = new EleEquipment(id);
		equipment.setDelFlag(User.DEL_FLAG_AUDIT);
		Result result = eleEquipmentService.checkEquipment(equipment);
		String locale = I18nUtils.getLocaleStr();
		String msg = "已经重新提交审核";
		if(Global.US.equals(locale)){
			msg = "The review has been re submitted";
		}
		addMessage(redirectAttributes, msg);
	/*	addMessage(redirectAttributes, "已经重新提交审核!");*/
		return "redirect:" + adminPath + "/equipment/eleEquipment/info?id=" + id;
	}

	@RequiresPermissions("equipment:eleEquipment:check")
	@RequestMapping(value = "check")
	public String check(String id, String delFlag, RedirectAttributes redirectAttributes){
		EleEquipment equipment = new EleEquipment(id);
		equipment.setDelFlag(delFlag);
		equipment.setCheckDate(new Date());
		equipment.setCheckBy(UserUtils.getUser());
		Result result = eleEquipmentService.checkEquipment(equipment);
		addMessage(redirectAttributes, result.getMsg());
		return "redirect:" + adminPath + "/equipment/eleEquipment/info?id=" + id;
	}

    @RequiresPermissions("equipment:eleEquipment:check")
    @RequestMapping("checkList")
    @ResponseBody
    public Result checkList(String equipmentList, String flag){
        String equipments = equipmentList.substring(0, equipmentList.length()-1);

        System.out.println(equipments);

        String status;
        String msg;
        if("delete".equals(flag)){
            status = EleEquipment.DEL_FLAG_DELETE;
			String locale = I18nUtils.getLocaleStr();
			msg = "删除成功";
			if(Global.US.equals(locale)){
				msg = "Deleted";
			}
	        eleEquipmentService.delApplyEquipmentStatus(equipments, status);
	        return new Result(Result.SUCCESS, msg, null);

        }
        if("reject".equals(flag)){
            status = EleEquipment.DEL_FLAG_AUDIT_NO;
			String locale = I18nUtils.getLocaleStr();
			msg = "未通过审核";
			if(Global.US.equals(locale)){
				msg = "Not passed";
			}
        }
        else{
            status = EleEquipment.DEL_FLAG_NORMAL;
			String locale = I18nUtils.getLocaleStr();
			msg = "审核通过";
			if(Global.US.equals(locale)){
				msg = "passed";
			}
        }
        User thisuser = UserUtils.getUser();
        eleEquipmentService.bathcUpdateEquipmentStatus(equipments, status, thisuser.getId(), "");

        return new Result(Result.SUCCESS, msg, null);
    }
    @RequestMapping("exists")
    @ResponseBody
    public Result exists(String mac){
    	EleEquipment eleEquipment = eleEquipmentService.findEquipmentConfigByCode(mac);
    	String msg = "设备ID重复，请重新输入";
    	if(eleEquipment != null){
            return new Result(Result.FAILURE, msg, null);

    	}else{
    		msg = "设备不存在，可以添加";
    		return new Result(Result.SUCCESS, msg, null);
        }
    }
	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = "monitor")
	public String monitor(EleEquipment eleEquipment, Model model, HttpServletRequest request) {
		model.addAttribute("eleEquipment", eleEquipment);
		List<EleMonitorParam> params = eleMonitorParamService.findEleMonitorParamByEqId(eleEquipment.getType().getId());
		model.addAttribute("params", params);
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
		model.addAttribute("language", locale.getLanguage());
//		List<EleSource> sources = eleSourceGroupService.findSourceOnEquipmentByType(eleEquipment.getId(), "0", "program");
//		List<EleSourceProgram> programs = new ArrayList<>();
//		for(EleSource s:sources){
//			if(!StringUtils.isEmpty(s.getContent())){
//				programs.add(eleSourceProgramService.get(s.getContent()));
//			}
//		}
//		model.addAttribute("programs", programs);
		return "modules/equipment/eleEquipmentMonitor";
	}

	@RequiresPermissions("equipment:eleEquipment:view")
	@RequestMapping(value = "floor_mapping")
	public String floor_mapping(EleEquipment eleEquipment, Model model) {
        model.addAttribute("eleEquipment", eleEquipment);

        String floorMapping = eleEquipment.getFloorMapping();
        model.addAttribute("floorMapping", JSONArray.parseArray(floorMapping));

        return "modules/equipment/eleEquipmentFloor";
	}

    @RequiresPermissions("equipment:eleEquipment:view")
    @RequestMapping(value = "floorsave")
    public String floorsave(EleEquipment eleEquipment, Model model) {
        Floor floor = eleEquipment.getFloor();
        System.out.println(floor);
        if(floor != null){
        String floorsStr = floor.getFloor();
        String tosStr = floor.getTo();

        JSONArray floorList = new JSONArray();

        String[] floors = floorsStr.split(",");
        String[] tos = tosStr.split(",");

        for(int i=0;i<floors.length;i++){
            JSONObject temFloor = new JSONObject();
            temFloor.put("floor", floors[i]);
            temFloor.put("to", tos[i]);
            floorList.add(temFloor);
        }
        eleEquipment.setFloorMapping(floorList.toJSONString());
        }else{
            eleEquipment.setFloorMapping(null);
        }
        eleEquipmentService.saveFloorMapping(eleEquipment);
        return "redirect:" + adminPath + "/equipment/eleEquipment/floor_mapping?id=" + eleEquipment.getId();
    }
    public List<User> getSubListForUserForCheck(User current){
		User user = new User();
		user.setDelFlag(null);
		List<User> list = systemService.findList(user);
		Role role = current.getRole();
		List<User> resultList = new ArrayList<User>();
		if (current.getUserType().equals("1")) {
			for (User usr : list) {
				if (!usr.getUserType().equals("1")) {
					resultList.add(usr);
				}
			}
		} else if (current.getUserType().equals("4")) {
			//resultList.add(current);
		} else if (current.getUserType().equals("3")) {
			for (User usr : list) {
				if (usr.getUserType().equals("4")
						&& usr.getCompany().getId().equals(current.getCompany().getId()) && usr.getOffice().getId().equals(current.getOffice().getId())) {
					resultList.add(usr);
				}
			}
			//resultList.add(current);
		} else {
			Set<String> offices = new HashSet<String>();
			for (User usr : list) {
				if (usr.getUserType().equals("3")
						&& usr.getCompany().getId().equals(current.getOffice().getId())) {
					resultList.add(usr);
					offices.add(usr.getOffice().getId());
				}
			}
			offices.add(current.getOffice().getId());
			for (User usr : list) {
				if (usr.getUserType().equals("4") && offices.contains(usr.getCompany().getId())) {
					resultList.add(usr);
				}
			}
			//resultList.add(current);
		}
		for (User temuser : resultList) {
			temuser.setRemarks(User.DEL_FLAG_NORMAL.equals(temuser.getDelFlag()) ? "正常" : "审核中");
		}
		return resultList;
	}
}